<?php
class BlogModel extends Model{
	public $table='blog';
	
	//获取博客
	public function getALL(){
		$sql="select count(*) as c from blog";
		$res=$this->query($sql);
		$page=new Page($res[0]['c'],2);
		$p=$page->show();
		$limit=$page->limit();
		$sql="select * from category c join blog b on b.cid=c.cid limit {$limit}";
		$data=$this->query($sql);
		return array('page'=>$p,'data'=>$data);
	}

	//添加博客
	public function add($data){
		if(!empty($_FILES['thumb']['name'])){
			$upload=new Upload;
			$u=$upload->upload();
			$data['thumb']=$u[0]['path'];
		}
		$data['addtime']=time();
		return $this->insert($data);
	}

	//修改博客

	public function edit($data){
		// p($_FILES);
		// p($data);exit;
		if(!empty($_FILES['thumb']['name'])){
			if(is_file($data['thumb'])){
				unlink($data['thumb']);
			}
			$upload=new Upload;
			$u=$upload->upload();
			$data['thumb']=$u[0]['path'];
		}
		$data['addtime']=time();
		return $this->where("bid={$data['bid']}")->update($data);
	}

	//删除博客
	public function del(){
		return $this->where("bid={$_GET['bid']}")->delete();
	}
}
?>